import {messages} from "share/common";
import React from 'react';
import {connect} from 'react-redux';
import {Card, Button, Form, Checkbox, Row, Radio, message} from 'antd';
import FormItemAirlineCabin from './components/form-item-airline-cabin';
import airlineConfigService from './airline-config.service';

class AirlineConfigBooking extends React.Component {
  static get controlDimensions() {
    return [
      'isControlDate',
      'isControlPassenger',
      'isControlSite',
      'isControlCabinClass',
      'isControlDiscount',
      'isControlPreBook',
      'isControlPrice'
    ];
  }
  constructor(props) {
    super(props);
    this.state = {
      submitting: false
    };
  }
  componentDidMount = () => {
    const { companyOID } = this.props;
    airlineConfigService.getConfig(companyOID)('bookControl').then((res) => {
      if (res.data.code !== 200) return Promise.reject(res.data.message);
      const data = res.data.body;
      const controlDimensions = this.constructor.controlDimensions;
      const dimension = Object.entries(data).reduce((p, c) => {
        if (controlDimensions.indexOf(c[0]) > -1 && c[1]) {
          p.push(c[0]);
        }
        return p;
      }, []);
      this.props.form.setFieldsValue({
        dimension,
        isMandatoryControl: Number(data.isMandatoryControl),
        isControlApprovalAgain: Number(data.isControlApprovalAgain),
        approval: {
          isEnable: data.isNotApprovalBook,
          controlType: +data.whiteListUserGroupId ? 2 : 1,
          userGroupId: data.whiteListUserGroupId || ''
        }
      });
    });
  }
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((error, values) => {
      if (error) return;
      const { companyOID } = this.props;
      const data = {};
      data.isNotApprovalBook = values.approval.isEnable;
      data.whiteListUserGroupId = values.approval.controlType === 2
        ? values.approval.userGroupId
        : '';
      data.isMandatoryControl = !!values.isMandatoryControl;
      data.isControlApprovalAgain = !!values.isControlApprovalAgain;
      const controlDimensions = this.constructor.controlDimensions;
      controlDimensions.forEach((key) => {
        data[key] = values.dimension.indexOf(key) > -1;
      });
      this.setState({ submitting: true });
      airlineConfigService.updateCompanyConfig(companyOID)(data).then((res) => {
        if (res.data.code !== 200) return Promise.reject();
        return message.success(messages('common.operate.success')/*操作成功*/);
      }).catch(() => {
        message.error(messages('common.operate.filed')/*操作失败*/);
      }).finally(() => {
        this.setState({ submitting: false });
      });
    });
  }

  render = () => {
    const { submitting } = this.state;
    const { getFieldDecorator } = this.props.form;
    return (<div className="airline-config-container">
      <Form onSubmit={this.handleSubmit}>
        <Form.Item>
          {getFieldDecorator('approval')(
            <FormItemAirlineCabin
              title={messages('supplier.key232')/*是否需要审批单设置*/}
              description={messages('supplier.key233')/*默认所有员工预订时需要审批单*/}
              text={messages('supplier.key234')/*部分员工预订时无需审批单*/}
            />)
          }
        </Form.Item>
        <Card type="inner" title={messages('supplier.key235')/*审批单校验维度设置*/} >
          <p className="hint">{messages('supplier.key236')/*选择后，员工预订时会校验航班信息与审批单信息是否一致。若员工预订时无需审批单，即使预订时选择了审批单也不校验*/}</p>
          <Form.Item>
            {getFieldDecorator('dimension')(
              <Checkbox.Group initialValue={[]}>
                <p><Checkbox value="isControlDate">{messages('supplier.key237')/*日期控制(1.往返行程会同时校验出发日期与返回日期；2.会兼容审批单中的浮动日期)*/}</Checkbox></p>
                <p><Checkbox value="isControlPassenger">{messages('supplier.key238')/*乘机人控制(乘机人需与审批单中参与人一致，且同一审批人不能重复订票)*/}</Checkbox></p>
                <p><Checkbox value="isControlSite">{messages('supplier.key239')/*出发/到达地控制(机票出发地与到达地需与审批单一致，支持审批单地点集控制)*/}</Checkbox></p>
                <p><Checkbox value="isControlCabinClass">{messages('supplier.key240')/*舱等控制(机票舱等不能超过审批单上的舱等)*/}</Checkbox></p>
                <p><Checkbox value="isControlDiscount">{messages('supplier.key241')/*折扣控制(机票折扣率不能超过审批单上机票折扣)*/}</Checkbox></p>
                <p><Checkbox value="isControlPreBook">{messages('supplier.key482')/*提前预订天数*/}</Checkbox></p>
                <p><Checkbox value="isControlPrice">{messages('common.amount')/*金额*/}</Checkbox></p>
              </Checkbox.Group>
            )}
          </Form.Item>
        </Card>
        <Card type="inner" title={messages('supplier.key242')/*订单提交时管控强度设置*/}>
          <p className="hint">{messages('supplier.key243')/*强管控：校验不通过，不可提交；弱管控：校验不通过时，员工需填写相应理由*/}</p>
          <Form.Item>
            {getFieldDecorator('isMandatoryControl')(
              <Radio.Group initialValue={0}>
                <Radio value={1}>{messages('supplier.key244')/*强管控*/}</Radio>
                <Radio value={0}>{messages('supplier.key245')/*弱管控*/}</Radio>
              </Radio.Group>
            )}
          </Form.Item>
        </Card>
        <Card type="inner" title={messages('supplier.key246')/*强管控二次审批设置*/}>
          <p className="hint">{messages('supplier.key247')/*启用强管控后，若启用二次审批功能，超标时员工可提交审批。审批通过后，可直接出票*/}</p>
          <Form.Item>
            {getFieldDecorator('isControlApprovalAgain')(
              <Radio.Group initialValue={0}>
                <Radio value={0}>{messages('supplier.key248')/*不启用*/}</Radio>
                <Radio value={1}>{messages('common.status.enable')/*启用*/}</Radio>
              </Radio.Group>
            )}
          </Form.Item>
          <p className="hint">{messages('supplier.key249')/*审批流程可在表单管理进行设置*/}</p>
        </Card>
        <Form.Item>
          <Button
            type="primary"
            htmlType="submit"
            loading={submitting}
          >{messages('supplier.key250')/*保存配置*/}</Button>
        </Form.Item>
      </Form>
    </div>);
  }
}

function mapStateToProps() {
  return {};
}

const WrappedAirlineConfigBooking = Form.create()(AirlineConfigBooking);

export default connect(mapStateToProps)(WrappedAirlineConfigBooking);
